package com.feilo.action;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.feilo.biz.AdminBiz;
import com.feilo.biz.ProductBiz;
import com.feilo.biz.UserBiz;
import com.feilo.biz.impl.AdminBizImpl;
import com.feilo.biz.impl.ProductBizImpl;
import com.feilo.biz.impl.UserBizImpl;
import com.feilo.entity.Admin;
import com.feilo.entity.Product;
import com.feilo.entity.User;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.zoo.pojo.Page;
@Controller
@Scope("prototype")
public class PublicAction extends ActionSupport{
	/**
	 * 
	 */
	private static final long serialVersionUID = -4751702987598358718L;
	private String msg;
	@Resource
	UserBiz us=new UserBizImpl();
	AdminBiz as=new AdminBizImpl();
	ProductBiz ps =new ProductBizImpl();
	private List<Product> products1;
	private List<Product> products2;
	private List<Product> products;
	private Product product;
	User user;
	Admin admin;
	//閿熸枻鎷烽〉閿熸枻鎷锋伅閿熸枻鎷烽敓鏂ゆ嫹閿熸枻鎷峰彇閿熸枻鎷穚ageInfo
	private Page pageInfo; 
    private int pageNo = 1; 
	int starts,ends,record,page;
	private String url ;
	private String skipUrl;
	private String text;
	String ip=ServletActionContext.getRequest().getRemoteAddr();
	private int showCount;
	/**
	 * 閿熸枻鎷烽敓鏂ゆ嫹娌￠敓锟�
	 */
	public String reg(){
		List<User> users=us.getUsers();
		if(users.size()==0){
			user.setU_level("閿熸枻鎷峰憳");
			user.setU_addtime(new Date());
			us.save(user);
			this.msg="娉ㄩ敓鏂ゆ嫹鏅掗敓鏂ゆ嫹閿熸枻鎷烽敓锟�";
			this.skipUrl="../logo_index.do";
			this.text="閿熸枻鎷烽〉";
			return "message";
		}
		for(User u:users){
			if(u.getU_username().trim().equals(user.getU_username())){
				this.msg="娉ㄩ敓鏂ゆ嫹澶遍敓鏉帮綇鎷烽敓鏂ゆ嫹";
				break;
			}else{
				this.msg="娉ㄩ敓鏂ゆ嫹鏅掗敓鏂ゆ嫹閿熸枻鎷烽敓锟�";
				continue;
			}
		}
		if(msg.equals("娉ㄩ敓鏂ゆ嫹鏅掗敓鏂ゆ嫹閿熸枻鎷烽敓锟�")){
			user.setU_level("閿熸枻鎷峰憳");
			user.setU_addtime(new Date());
			us.save(user);
			this.msg="娉ㄩ敓鏂ゆ嫹鏅掗敓鏂ゆ嫹閿熸枻鎷烽敓锟�";
			this.skipUrl="../logo_index.do";
			this.text="閿熸枻鎷烽〉";
			return "message";
		}else{
			this.msg="娉ㄩ敓鏂ゆ嫹澶遍敓鏉帮綇鎷烽敓鏂ゆ嫹閿熺煫浼欐嫹閿熺獤鎲嬫嫹娉ㄩ敓缁擄紒閿熸枻鎷�";
			this.skipUrl="reg.jsp";
			this.text="娉ㄩ敓鏂ゆ嫹椤甸敓鏂ゆ嫹";
			return "message";
		}
		
	}
	/**
	 * 閿熺煫浼欐嫹閿熸枻鎷峰綍閿熸枻鎷烽敓鏂ゆ嫹
	 * @return
	 */
	public String userLogin(){
		List<User> users=us.getUsers();
		for(User u:users){
			if(u.getU_username().trim().equals(user.getU_username())&&u.getU_password().equals(user.getU_password())){
				ActionContext.getContext().getSession().put("name", u.getU_username());
				ActionContext.getContext().getSession().put("exits", "娉ㄩ敓鏂ゆ嫹");
				ActionContext.getContext().getSession().remove("logo");
				this.msg="閿熸枻鎷峰綍閿熺即鐧告嫹閿熸枻鎷烽敓鏂ゆ嫹";
				this.skipUrl="page/index.jsp";
				this.text="閿熸枻鎷烽〉";
				break;
			}else{
				this.msg="閿熸枻鎷峰綍澶遍敓鏉帮綇鎷烽敓鏂ゆ嫹閿熸枻鎷烽敓鏂ゆ嫹閿熸枻鎷峰睅钘曢敓鏂ゆ嫹閿熸枻鎷烽敓鏂ゆ嫹閿熸枻鎷烽敓鏂ゆ嫹閿熻涓嶉敓鏂ゆ嫹";
				this.skipUrl="page/index.jsp";
				this.text="閿熺煫浼欐嫹閿熸枻鎷峰綍";
				continue;
			}
		}
		return "message";
	}
	/**
	 * 閿熸枻鎷峰彴閿熸枻鎷烽敓鏂ゆ嫹鍛橀敓鏂ゆ嫹褰�
	 * @return
	 */
	public String adminLogin(){
		List<Admin> admins=as.getAdmins();
		for(Admin ad:admins){
			if(ad.getAd_username().trim().equals(admin.getAd_username())&&ad.getAd_password().equals(admin.getAd_password())){
				ActionContext.getContext().getSession().put("user", ad.getAd_username());
				ActionContext.getContext().getSession().put("exit", "娉ㄩ敓鏂ゆ嫹");
				ActionContext.getContext().getSession().put("close", "閿熸埅鎲嬫嫹");
				this.msg="閿熸枻鎷峰綍閿熺即鐧告嫹閿熸枻鎷烽敓鏂ゆ嫹";
				this.skipUrl="console/admin_main.html";
				this.text="閿熸枻鎷峰彴閿熸枻鎷烽敓鏂ゆ嫹閿熸枻鎷烽〉";
				break;
			}else{
				this.msg="閿熸枻鎷峰綍澶遍敓鏉帮綇鎷烽敓鏂ゆ嫹閿熸枻鎷烽敓鏂ゆ嫹閿熸枻鎷峰睅钘曢敓鏂ゆ嫹閿熸枻鎷烽敓鏂ゆ嫹閿熸枻鎷烽敓鏂ゆ嫹閿熻涓嶉敓鏂ゆ嫹";
				this.skipUrl="console/login.html";
				this.text="閿熸枻鎷峰彴閿熸枻鎷烽敓鏂ゆ嫹閿熼摪锟�";
				continue;
			}
		}
		return "message";
	}
	public String exit(){
		ActionContext.getContext().getSession().remove("user");
		ActionContext.getContext().getSession().remove("exit");
		this.msg="閿熷壙绛规嫹鏅掗敓鏂ゆ嫹閿熸枻鎷烽敓锟�";
		this.skipUrl="console/login.html";
		this.text="閿熸枻鎷峰彴閿熸枻鎷烽敓鏂ゆ嫹閿熼摪瀹剁鎷烽敓锟�";
		return "message";
	}
	public String exits(){
		ActionContext.getContext().getSession().remove("name");
		ActionContext.getContext().getSession().remove("exits");
		this.msg="閿熷壙绛规嫹鏅掗敓鏂ゆ嫹閿熸枻鎷烽敓锟�";
		this.skipUrl="logo_index.do";
		this.text="閿熸枻鎷烽敓杈冪櫢鎷烽敓鏂ゆ嫹閿熸暀绛规嫹閿熸枻鎷烽〉";
		return "message";
	}
	/**
	 * 閿熸枻鎷疯浆閿熸枻鎷烽敓鏂ゆ嫹椤�
	 */
	public String index(){
		String name=(String) ActionContext.getContext().getSession().get("name");
		if(name==null||"".equals(name)){
			ActionContext.getContext().getSession().put("logo", "閿熸枻鎷峰綍");
		}
		List<Product> list=ps.getProducts();
		products1=ps.getProducts("閿熻浼欐嫹").subList(0, 4);
		products2=ps.getProducts("閿熸枻鎷烽敓鏂ゆ嫹").subList(0, 4);
        url="logo_index.do";   
		return "index";
	}
	/**
	 * 閿熸枻鎷烽敓鏂ゆ嫹閿熺嫛鍑ゆ嫹鏂滈敓锟�
	 */
	public String search(){
		List<Product> list = ps.getProductsBySerch(product.getP_name());
		products1=ps.getProducts("閿熻浼欐嫹").subList(0,4);
		products2=ps.getProducts("閿熸枻鎷烽敓鏂ゆ嫹").subList(0,4);
        url="logo_search.do";  
		return "index";
	}
	/**
	 * 閿熸枻鎷风ず閿熸枻鎷峰搧閿熸枻鎷风粏閿熸枻鎷锋伅
	 * @return
	 */
	public String viewProduct(){
		this.product=ps.getProduct(product.getId());
		return "viewProduct";
	}

	
	/*getter 閿熸枻鎷穝etter閿熸枻鎷烽敓鏂ゆ嫹*/
	public String getMsg() {
		return msg;
	}
	public void setMsg(String msg) {
		this.msg = msg;
	}
	public User getUser() {
		return user;
	}
	public void setUser(User user) {
		this.user = user;
	}
	public Admin getAdmin() {
		return admin;
	}
	public void setAdmin(Admin admin) {
		this.admin = admin;
	}
	public String getSkipUrl() {
		return skipUrl;
	}
	public void setSkipUrl(String skipUrl) {
		this.skipUrl = skipUrl;
	}
	public String getText() {
		return text;
	}
	public void setText(String text) {
		this.text = text;
	}

	public int getPageNo() {
		return pageNo;
	}
	public void setPageNo(int pageNo) {
		this.pageNo = pageNo;
	}
	public String getUrl() {
		return url;
	}
	public void setUrl(String url) {
		this.url = url;
	}
	public ProductBiz getPs() {
		return ps;
	}
	public void setPs(ProductBiz ps) {
		this.ps = ps;
	}
	public List<Product> getProducts() {
		return products;
	}
	public void setProducts(List<Product> products) {
		this.products = products;
	}
	public List<Product> getProducts1() {
		return products1;
	}
	public void setProducts1(List<Product> products1) {
		this.products1 = products1;
	}
	public List<Product> getProducts2() {
		return products2;
	}
	public void setProducts2(List<Product> products2) {
		this.products2 = products2;
	}
	public Product getProduct() {
		return product;
	}
	public void setProduct(Product product) {
		this.product = product;
	}
	public Page getPageInfo() {
		return pageInfo;
	}
	public void setPageInfo(Page pageInfo) {
		this.pageInfo = pageInfo;
	}
	public int getShowCount() {
		return showCount;
	}
	public void setShowCount(int showCount) {
		this.showCount = showCount;
	}
	
}
